#ifndef _UTILS_H
#define _UTILS_H
# include <iostream>
# include <sys/time.h>

// ================================================================
// A chronometer class: Start() and Stop() are the main functions.
class Chrono {
 private:
  timeval start;
  bool started;

 public:
  // ================================================================
  Chrono() : started(false) {};
  // ================================================================
  ~Chrono() {
    if(started) Stop(); // automatic stop ...
  }
  // ================================================================
  void Start() {
    if( !started ) {
      started = true;
      gettimeofday( &start, NULL );
    }
  }
  // ================================================================
  void Stop() {
    if ( started ) {
      started = false;
      timeval stop;
      gettimeofday( &stop, NULL );
      const double sec = (stop.tv_sec - start.tv_sec) 
	+ 1e-6f*(stop.tv_usec - start.tv_usec);
      std::cout<<"Ellapsed time: "<<sec<<" second"<<(sec>=2 ? "s" : "")<<std::endl;
    }
  }
};

#endif
